package com.zy.dubbo.demo.component;

import com.zy.dubbo.demo.constants.ResponseMsg;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * I18NUtil
 *
 * @author Robbin Zhao
 * @since 2020-12-10
 */
@Component
public class I18nUtil {

    @Resource
    private MessageSource messageSource;

    public String getI18nMsg(String msg) {
        Locale local = LocaleContextHolder.getLocale();
        return messageSource.getMessage(msg, null, msg, local);
    }

    public String getI18nMsg(ResponseMsg responseMsg) {
        return getI18nMsg(responseMsg.getMsg());
    }
}
